home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / circuits / irsim-ca.2 / irsim-ca / irsim-cap-9.2 / src / ana11 / textwind.c < prev    next >
C/C++ Source or Header  |  1993-01-15  |  6KB  |  273 lines

  1. /*
  2.  *     ********************************************************************* 
  3.  *     * Copyright (C) 1988, 1990 Stanford University.                     * 
  4.  *     * Permission to use, copy, modify, and distribute this              * 
  5.  *     * software and its documentation for any purpose and without        * 
  6.  *     * fee is hereby granted, provided that the above copyright          * 
  7.  *     * notice appear in all copies.  Stanford University                 * 
  8.  *     * makes no representations about the suitability of this            * 
  9.  *     * software for any purpose.  It is provided "as is" without         * 
  10.  *     * express or implied warranty.  Export of this software outside     * 
  11.  *     * of the United States of America may require an export license.    * 
  12.  *     *********************************************************************
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <varargs.h>
  17. #include "ana.h"
  18. #include "ana_glob.h"
  19. #include "graphics.h"
  20.  
  21.  
  22. #define    BUFFSIZE    256
  23.  
  24.  
  25. private char    buffer[ BUFFSIZE ];    /* text widow buffer */
  26. private int     txtPos = 0;        /* postion to print next character */
  27. private    char    txt_cursor[] = " ";    /* cursor: a space in inverse video */
  28. private    int     cursor_on = FALSE;    /* TRUE when cursor on */
  29. private    int     maxPos;
  30.  
  31.  
  32. #define    XPOS( Pos )        ( ((Pos) * CHARWIDTH) + 2 )
  33. #define    YPOS            ( textBox.bot - 1 - descent )
  34. #define    CWIDTH( X )        ( (X) * CHARWIDTH )
  35. #define    TXT_HEIGHT        ( textBox.bot - textBox.top )
  36.  
  37.  
  38. #define    PTEXT( Text, N, Len )            XDrawImageString \
  39.     ( display, window, gcs.black, XPOS( N ), YPOS, Text, Len )
  40.  
  41. #define    PCURSOR()                XDrawImageString \
  42.     ( display, window, gcs.white, XPOS( txtPos ), YPOS, txt_cursor, 1 )
  43.  
  44. #define    ERASE_CURSOR()                            \
  45.     PTEXT( txt_cursor, txtPos, 1 )
  46.  
  47.  
  48. public void RedrawText()
  49.   {
  50.     maxPos = textBox.right / CHARWIDTH;
  51.     if( maxPos > BUFFSIZE )
  52.     maxPos = BUFFSIZE;
  53.     FillBox( window, textBox, gcs.white );
  54.     HLine( window, textBox.left, textBox.right, textBox.top, gcs.black );
  55.     if( txtPos )
  56.     PTEXT( buffer, 0, txtPos );
  57.     if( cursor_on )
  58.     PCURSOR();
  59.   }
  60.  
  61.  
  62. public void PRINT( s )
  63.   char  *s;
  64.   {
  65.     int  len;
  66.  
  67.     if( *s == '\n' )
  68.       {
  69.     if( txtPos > 0 )
  70.         FillAREA( window, XPOS( 0 ), textBox.top + 1, CWIDTH( txtPos ),
  71.          TXT_HEIGHT, gcs.white );
  72.     txtPos = 0;
  73.     s++;
  74.       }
  75.     len = strlen( s );
  76.     if( txtPos + len >= BUFFSIZE )
  77.       {
  78.     len = BUFFSIZE - 1 - txtPos;
  79.       }
  80.     if( len > 0 )
  81.       {
  82.     bcopy( s, buffer + txtPos, len );
  83.     PTEXT( s, txtPos, len );
  84.     txtPos += len;
  85.       }
  86.   }
  87.  
  88.  
  89. /* VARARGS */
  90. public void PRINTF( va_alist )
  91.   va_dcl
  92.   {
  93.     va_list  args;
  94.     char     *format;
  95.     char     *s;
  96.     int      len;
  97.  
  98.     va_start( args );
  99.     format = va_arg( args, char * );
  100.  
  101.     if( *format == '\n' )
  102.       {
  103.     if( txtPos > 0 )
  104.         FillAREA( window, XPOS( 0 ), textBox.top + 1, CWIDTH( txtPos ),
  105.          TXT_HEIGHT, gcs.white );
  106.     txtPos = 0;
  107.     format++;
  108.     s = buffer;
  109.       }
  110.     else
  111.     s = buffer + txtPos;
  112.  
  113.     (void) vsprintf( s, format, args );
  114.     len = strlen( s );
  115.     PTEXT( s, txtPos, len );
  116.     txtPos += len;
  117.   }
  118.  
  119.  
  120. private    Func    FQuerying;
  121. private    char    *inpStart;
  122. private    void    StrInput();
  123.  
  124.  
  125. public void Query( prompt, func )
  126.   char   *prompt;
  127.   Func   func;
  128.   {
  129.     PRINT( prompt );
  130.     inpStart = buffer + txtPos;
  131.     cursor_on = TRUE;
  132.     FQuerying = func;
  133.     PCURSOR();
  134.     SendEventTo( StrInput );
  135.   }
  136.  
  137.  
  138.  
  139.     /* define some interesting control characters */
  140.  
  141. #define    BACKSPACE    '\b'
  142. #define DELETE        '\177'
  143. #define    TAB        '\t'
  144. #define    RETURN        '\r'
  145. #define    NEWLINE        '\n'
  146. #define    CTRL_C        '\003'
  147. #define    CTRL_U        '\025'
  148. #define    CTRL_W        '\027'
  149.  
  150.  
  151. private int Cancel()
  152.   {
  153.     int  left;
  154.  
  155.     bcopy( "(canceled)", inpStart, 11 );
  156.     left = XPOS( inpStart - buffer + 10 );
  157.     FillAREA( window, left, textBox.top + 1, textBox.right - left + 1,
  158.      TXT_HEIGHT, gcs.white );
  159.     txtPos = inpStart - buffer;
  160.     inpStart = NULL;
  161.     return( txtPos + 10 );
  162.   }
  163.  
  164.  
  165. private void StrInput( ev )
  166.   XKeyEvent  *ev;
  167.   {
  168.     char           buff[ 40 ];
  169.     register char  *iPtr, *p;
  170.     register int   newPos, len;
  171.     int            nChars, finish = FALSE;
  172.  
  173.     if( ev == NULL )
  174.       {
  175.     cursor_on = FALSE;
  176.     ERASE_CURSOR();
  177.     txtPos = 0;
  178.     (*FQuerying)( NULL );
  179.     return;
  180.       }
  181.     
  182.     if( ev->type != KeyPress )
  183.       {
  184.     XBell( display, 0 );
  185.     return;
  186.       }
  187.  
  188.     newPos = txtPos;
  189.     iPtr = buffer + newPos;
  190.     nChars = XLookupString( ev, buff, sizeof( buff ), NULL, NULL );
  191.  
  192.     for( p = buff, len = nChars; len > 0 and not finish; p++, len-- )
  193.       {
  194.         switch( *p )
  195.       {
  196.         case BACKSPACE :
  197.         case DELETE :
  198.         if( iPtr > inpStart )
  199.           {
  200.             newPos--;
  201.             iPtr--;
  202.           }
  203.         break;
  204.         case RETURN :
  205.         case NEWLINE :
  206.         *iPtr = '\0';
  207.         finish = TRUE;
  208.         break;
  209.  
  210.         case TAB :
  211.         *iPtr++ = ' ';
  212.         newPos++;
  213.         break;
  214.  
  215.         case CTRL_C :
  216.         newPos = Cancel();
  217.         finish = TRUE;
  218.         break;
  219.  
  220.         case CTRL_U :
  221.         if( iPtr > inpStart )
  222.           {
  223.             newPos = inpStart - buffer;
  224.             iPtr = inpStart;
  225.           }
  226.          break;
  227.  
  228.         case CTRL_W :
  229.         if( iPtr > inpStart )
  230.           {
  231.             iPtr--;
  232.             while( iPtr > inpStart and *iPtr == ' ' )
  233.             iPtr--;
  234.             while( iPtr > inpStart and *iPtr != ' ' )
  235.             iPtr--;
  236.             if( iPtr != inpStart )
  237.             iPtr++;
  238.             newPos = iPtr - buffer;
  239.           }
  240.         break;
  241.         default :
  242.         if( *p >= ' ' )        /* displayable character */
  243.           {
  244.             if( newPos < maxPos )
  245.               {
  246.             *iPtr++ = *p;
  247.             newPos++;
  248.               }
  249.             else
  250.             XBell( display, 0 );
  251.           }
  252.       }
  253.       }
  254.  
  255.     if( newPos < txtPos )
  256.     FillAREA( window, XPOS( newPos ), textBox.top + 1, 
  257.      CWIDTH( txtPos - newPos + 1 ), TXT_HEIGHT, gcs. white );
  258.     else if( newPos > txtPos )
  259.     PTEXT( buffer + txtPos, txtPos, newPos - txtPos );
  260.  
  261.     txtPos = newPos;
  262.  
  263.     if( finish )
  264.       {
  265.     SendEventTo( NULL );
  266.     cursor_on = FALSE;
  267.     ERASE_CURSOR();
  268.     (*FQuerying)( inpStart );
  269.       }
  270.     else
  271.     PCURSOR();
  272.   }
  273.